/*
 * pps_bom
 */
package com.pitop.project.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.ProjectException;
import com.pitop.core.tools.StringTools;
import com.pitop.project.api.ProjectCategoryFeignApi;
import com.pitop.project.api.ProjectFeignApi;
import com.pitop.project.entity.Project;
import com.pitop.project.entity.ProjectCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 项目
 *
 * @author berton
 */
@Service("projectSVImpl")
@Slf4j
public class ProjectSVImpl {

    @Autowired
    private ProjectFeignApi projectFeignApi;

    @Autowired
    private ProjectCategoryFeignApi categoryFeignApi;

    /**
     * 加载一个对象Project
     *
     * @param id * @param code 编码
     * @return Project
     */
    public Project load(Long id, String code) {
        if (id == null && code == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectFeignApi.load(id, code);
    }

    /**
     * 加载一个对象Project 通过id,(将查询关联数据)
     *
     * @param id
     * @return Project
     */
    public Project loadById(Long id) {
        if (id == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectFeignApi.loadById(id);
    }

    /**
     * 加载一个对象Project 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return Project
     */
    public Project loadByCode(String code) {
        if (code == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectFeignApi.loadByCode(code);
    }

    /**
     * 加载一个对象Project,(将查询关联数据)
     *
     * @param id * @param code 编码
     * @return Project
     */
    public Project get(Long id, String code) {
        if (id == null && code == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectFeignApi.get(id, code);
    }

    /**
     * 加载一个对象Project 通过id,(将查询关联数据)
     *
     * @param id
     * @return Project
     */
    public Project getById(Long id) {
        if (id == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectFeignApi.getById(id);
    }

    /**
     * 加载一个对象Project 通过code,(将查询关联数据)
     *
     * @param code 编码
     * @return Project
     */
    public Project getByCode(String code) {
        if (code == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return projectFeignApi.getByCode(code);
    }

    /**
     * 删除对象Project
     *
     * @param id * @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectFeignApi.delete(id, code);
    }

    /**
     * 查询Project分页
     *
     * @param project  对象
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Project>
     */
    public List<Project> list(Project project, int curPage, int pageSize) {
        return projectFeignApi.list(project, curPage, pageSize);
    }

    /**
     * 查询Project分页
     *
     * @param id       * @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Project>
     */
    public List<Project> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        Project project = new Project();
        project.setId(id);
        project.setCode(code);

        return projectFeignApi.list(project, curPage, pageSize);
    }

    public int count(Long id, String code) {
        return projectFeignApi.count(id, code);
    }

    public int count(Project project) {
        return projectFeignApi.count(project);
    }

    /**
     * 保存
     *
     * @param project 实体
     * @throws BaseException
     */
    public void save(Project project) {
        if (project == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //项目名是否已经存在
        Project projectFlag = projectFeignApi.loadByName(project.getName());
        if(projectFlag != null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Project_Name_Is_Exist);
        }
        //如果项目代码存在，判断项目代码是否已经存在
        if(StringTools.isNotEmpty(project.getProjectCode())) {
            projectFlag = projectFeignApi.loadByProjectCode(project.getProjectCode());
            if(projectFlag != null) {
                throw new ProjectException(BaseException.BaseExceptionEnum.Project_Code_Is_Exist);
            }
        }
        //项目分类是否存在
        ProjectCategory projectCategory = categoryFeignApi.loadByCode(project.getProjectCategoryCode());
        if(projectCategory == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Project_Category_Is_Not_Exist);
        }
        projectFeignApi.build(project);
    }

    /**
     * 更新
     *
     * @param project 实体
     * @throws BaseException
     */
    public void modify(Project project) {
        if (project == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //项目名是否已经存在
        Project projectFlag = projectFeignApi.loadByName(project.getName());
        if(projectFlag != null && !project.getCode().equals(projectFlag.getCode())) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Project_Name_Is_Exist);
        }
        //如果项目代码存在，判断项目代码是否已经存在
        if(StringTools.isNotEmpty(project.getProjectCode())) {
            projectFlag = projectFeignApi.loadByProjectCode(project.getProjectCode());
            if(projectFlag != null && !project.getCode().equals(projectFlag.getCode())) {
                throw new ProjectException(BaseException.BaseExceptionEnum.Project_Code_Is_Exist);
            }
        }
        //项目分类是否存在
        ProjectCategory projectCategory = categoryFeignApi.loadByCode(project.getProjectCategoryCode());
        if(projectCategory == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Project_Category_Is_Not_Exist);
        }
        projectFeignApi.modify(project);
    }

    /**
     * 复制
     *
     * @param code 项目编码
     * @param account
     *@param accountCode @throws BaseException
     */
    public void copy(String code, String account, String accountCode) {
        if (StringTools.isEmpty(code)) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //项目信息是否存在
        Project project = projectFeignApi.loadByCode(code);
        if(project == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Result_Not_Exist);
        }
        //项目是否已经被复制过
        String name = project.getName() + "_copy";
        //项目名是否已经存在
        Project projectFlag = projectFeignApi.loadByName(name);
        if(projectFlag != null && !project.getCode().equals(projectFlag.getCode())) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Project_Name_Is_Exist);
        }
        //如果项目代码存在，判断项目代码是否已经存在
        if(StringTools.isNotEmpty(project.getProjectCode())) {
            String projectCode = project.getProjectCode() + "_copy";
            projectFlag = projectFeignApi.loadByProjectCode(projectCode);
            if(projectFlag != null && !project.getCode().equals(projectFlag.getCode())) {
                throw new ProjectException(BaseException.BaseExceptionEnum.Project_Code_Is_Exist);
            }
        }
        projectFeignApi.copy(code, account, accountCode);
    }

    /**
     * 修改项目状态
     *  @param code  编码
     * @param state 状态
     */
    public void updateState(String code, String state) {
        if (StringTools.isEmpty(code) || state == null) {
            throw new ProjectException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        projectFeignApi.updateState(code, state);
    }
}
